package com.ruoyi.walmart.service.auth.impl;

import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.core.domain.Platform;
import com.ruoyi.common.core.domain.SiteForm;
import com.ruoyi.common.core.domain.TreeSelect;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.spring.SpringUtils;
import com.ruoyi.walmart.mapper.auth.SiteFormMapper;
import com.ruoyi.walmart.service.auth.SiteFormService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class SiteFormServiceImpl implements SiteFormService {
    @Autowired
    SiteFormMapper siteFormMapper;

    @Override
    @DataScope(SiteFormAlias="s")
    public List<SiteForm> selectAllList(SiteForm siteForm) {
        return siteFormMapper.selectAllList(siteForm);
    }

    @Override
    public List<TreeSelect> selectSiteFormTreeList(SiteForm siteForm) {
        List<SiteForm> siteFormList = SpringUtils.getAopProxy(this).selectAllList(siteForm);
        return buildSiteFormTreeSelect(siteFormList);
    }

    @Override
    public List<SiteForm> buildSiteFormTree(List<SiteForm> siteFormList) {
        List<SiteForm> returnList = new ArrayList<>();
        List<Long> collect = siteFormList.stream().map(SiteForm::getSiteId).collect(Collectors.toList());
        for (SiteForm siteForm : siteFormList){
            //如果是顶节点，遍历该父节点下的所以子节的
            if(!collect.contains(siteForm.getParentId())){
                recursionFn(siteFormList,siteForm);
                returnList.add(siteForm);
            }
        }
        if (returnList.isEmpty())
        {
            returnList = siteFormList;
        }
        return returnList;
    }
    /**
     * 递归列表
     * @param siteFormList 子级
     * @param siteForm 父级
     */
    private void recursionFn(List<SiteForm> siteFormList , SiteForm siteForm){
        // 得到子节点列表
        List<SiteForm> childList = getChildList(siteFormList, siteForm);
        siteForm.setChildrenSiteForm(childList);
        for (SiteForm newSiteForm : childList)
        {
            if (hasChild(siteFormList, newSiteForm))
            {
                recursionFn(siteFormList, newSiteForm);
            }
        }
    }
    /**
     * 获得子节点
     * @param siteFormList
     * @param siteForm
     * @return
     */
    private List<SiteForm> getChildList(List<SiteForm> siteFormList, SiteForm siteForm){
        List<SiteForm> returnList = new ArrayList<>();
        Iterator<SiteForm> iterator = siteFormList.iterator();
        while (iterator.hasNext()){
            SiteForm n = (SiteForm) iterator.next();
            if (StringUtils.isNotNull(n.getParentId()) && n.getParentId().longValue() == siteForm.getSiteId().longValue())
            {
                returnList.add(n);
            }
        }
        return returnList;
    }

    /**
     * 判断是否有子节点
     */
    private boolean hasChild(List<SiteForm> siteFormList, SiteForm siteForm)
    {
        return getChildList(siteFormList, siteForm).size() > 0;
    }

    @Override
    public List<TreeSelect> buildSiteFormTreeSelect(List<SiteForm> siteFormList) {
        List<SiteForm> SiteFormTrees = buildSiteFormTree(siteFormList);
        return SiteFormTrees.stream().map(TreeSelect::new).collect(Collectors.toList());
    }
}
